import createAxios, { requestPayload } from '/@/utils/axios'
import type { Method } from 'axios'

interface ReqOptions {
    // 透传给 createAxios 的第二个参数
    showSuccessMessage?: boolean
    showErrorMessage?: boolean
    showCodeMessage?: boolean
    loading?: boolean
    anotherToken?: string
}

export default {
    request(config: any, options: ReqOptions = {}) {
        return createAxios(config, options)
    },
    get(url: string, params: any = {}, options: ReqOptions = {}) {
        return createAxios({ url, method: 'get', params }, options)
    },
    post(url: string, data: any = {}, options: ReqOptions = {}) {
        return createAxios({ url, method: 'post', data }, options)
    },
    put(url: string, data: any = {}, options: ReqOptions = {}) {
        return createAxios({ url, method: 'put', data }, options)
    },
    delete(url: string, params: any = {}, options: ReqOptions = {}) {
        return createAxios({ url, method: 'delete', params }, options)
    },
    // 低级接口：根据 method 和 payload 组装参数
    raw(url: string, method: Method, data: any = {}, options: ReqOptions = {}) {
        const payload = requestPayload(method, data)
        return createAxios(Object.assign({ url, method }, payload), options)
    },
}
